package com.sfzd5.UI;

import com.github.jfasttext.JFastText;
import com.google.gson.Gson;
import com.sfzd5.aiclassroom.modle.ExamQuestionsInfo;
import com.sfzd5.aiclassroom.modle.Question;
import com.sfzd5.exam.helplibrary.Generator;
import com.sfzd5.fasttextknowledge.FSHelper;
import org.apache.commons.io.FileUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PredictKnowledgeDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JComboBox cb_subject;
    private JButton 识别知识点Button;
    private JLabel lb_msg;
    private JScrollPane qPanel;
    private JPanel qiPanel;

    Map<String, String> subjects;
    ExamQuestionsInfo task;
    HashMap<Long, Question> questionMap = new HashMap<>();
    HashMap<Long, QuestionKnowledgePanel> questionKnowledgePanelHashMap = new HashMap<>();

    public PredictKnowledgeDialog(ExamQuestionsInfo task) {
        this.task = task;

        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                saveData();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        VerticalFlowLayout vfl1 = new VerticalFlowLayout();
        vfl1.setVGap(10);
        qiPanel.setLayout(vfl1);

        for (List<Question> qs : task.content.values()) {
            for (Question q : qs) {
                QuestionKnowledgePanel p = new QuestionKnowledgePanel(q);
                qiPanel.add(p.mPanel);
                questionKnowledgePanelHashMap.put(q.id, p);
                questionMap.put(q.id, q);
            }
        }

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        识别知识点Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String subject_name = (String) cb_subject.getSelectedItem();
                String km = subjects.get(subject_name);
                FSHelper fs = new FSHelper(km);

                for (Question q : questionMap.values()) {
                    String txt = Generator.generateQuestionHtml_forPanelOnlyQuestion(q);
                    if (txt.length() < 10) {
                        lb_msg.setText("没有习题或习题太短");
                        continue;
                    }

                    List<JFastText.ProbLabel> ps = fs.Predict(txt, 5);
                    q.knowledges = new ArrayList<>();
                    if (ps.size() < 3) {
                        for (JFastText.ProbLabel p : ps)
                            q.knowledges.add(fs.GetKnowledgeName(p));
                    } else {
                        for (JFastText.ProbLabel p : ps) {
                            if (p.logProb < -5)
                                continue;
                            q.knowledges.add(fs.GetKnowledgeName(p));
                            /*
                            List<Knowledge> ks = fs.GetKnowledgePath(p);
                            StringBuilder sb = new StringBuilder();
                            for (Knowledge k : ks) {
                                sb.append(" > ");
                                sb.append(k.n);
                            }
                            */
                        }
                    }
                }

                for (QuestionKnowledgePanel p : questionKnowledgePanelHashMap.values()) {
                    p.reSetKnowledge();
                }
            }
        });

        subjects = new HashMap<String, String>();
        subjects.put("物理", "wl");
        subjects.put("地理", "dl");
        subjects.put("历史", "ls");
        for (String k : subjects.keySet()) {
            cb_subject.addItem(k);
        }
    }

    //保存数据
    private void saveData() {
        // add your code here
        File taskFile = new File(HomeWorkManager.examDir, task.id + ".json");
        String json = new Gson().toJson(task);
        try {
            FileUtils.writeStringToFile(taskFile, json, "utf-8");
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        dispose();
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(0, 0));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new BorderLayout(0, 0));
        contentPane.add(panel1, BorderLayout.CENTER);
        qPanel = new JScrollPane();
        panel1.add(qPanel, BorderLayout.CENTER);
        qiPanel = new JPanel();
        qiPanel.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        qPanel.setViewportView(qiPanel);
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 3, new Insets(0, 0, 0, 0), -1, -1));
        contentPane.add(panel2, BorderLayout.SOUTH);
        final com.intellij.uiDesigner.core.Spacer spacer1 = new com.intellij.uiDesigner.core.Spacer();
        panel2.add(spacer1, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
        final JPanel panel3 = new JPanel();
        panel3.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 4, new Insets(0, 0, 0, 0), -1, -1));
        panel2.add(panel3, new com.intellij.uiDesigner.core.GridConstraints(0, 2, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_BOTH, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        buttonOK = new JButton();
        buttonOK.setText("保存");
        panel3.add(buttonOK, new com.intellij.uiDesigner.core.GridConstraints(0, 2, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        buttonCancel = new JButton();
        buttonCancel.setText("取消");
        panel3.add(buttonCancel, new com.intellij.uiDesigner.core.GridConstraints(0, 3, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        识别知识点Button = new JButton();
        识别知识点Button.setText("识别知识点");
        panel3.add(识别知识点Button, new com.intellij.uiDesigner.core.GridConstraints(0, 1, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        cb_subject = new JComboBox();
        cb_subject.setMaximumSize(new Dimension(84, 30));
        cb_subject.setPreferredSize(new Dimension(84, 30));
        panel3.add(cb_subject, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        lb_msg = new JLabel();
        lb_msg.setText("");
        panel2.add(lb_msg, new com.intellij.uiDesigner.core.GridConstraints(0, 1, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return contentPane;
    }
}
